package cn.com.cym.gamezao.web.xmu;

import cn.com.cym.gamezao.annotation.AuthToken;
import cn.com.cym.gamezao.base.BaseController;
import cn.com.cym.gamezao.base.BaseService;
import cn.com.cym.gamezao.base.ResultResponse;
import cn.com.cym.gamezao.entity.LocalFile;
import cn.com.cym.gamezao.entity.xmu.Schedule;
import cn.com.cym.gamezao.exception.BaseException;
import cn.com.cym.gamezao.service.LocalFileService;
import cn.com.cym.gamezao.service.xmu.ScheduleService;
import cn.com.cym.gamezao.util.CommonUtils;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.*;
import java.util.List;

@Api(tags = "课程表提醒模块")
@RestController
@RequestMapping(value = "schedule")
@AuthToken(rule = "xmu.schedule")
public class ScheduleController extends BaseController<Schedule> {
    final ScheduleService thisService;

    final LocalFileService localFileService;

    public ScheduleController(ScheduleService scheduleService,LocalFileService localFileService){
        this.thisService=scheduleService;
        this.localFileService = localFileService;
    }

    @Override
    public BaseService<Schedule> getService() {
        return this.thisService;
    }

    @ApiImplicitParam(name = "schedule", value = "schedule", required = true,dataType = "Schedule")
    @ApiOperation(value = "新建")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @PostMapping()
    @AuthToken(rule = "add")
    public ResponseEntity<?> add(@RequestBody Schedule schedule){
        final Schedule scheduleStore = this.thisService.save(schedule);
        return ResponseEntity.ok(scheduleStore);
    }

    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "id",paramType = "path", required = true,dataType = "Long"),
            @ApiImplicitParam(name = "schedule", value = "schedule", required = true,dataType = "Schedule")})
    @ApiOperation(value = "更新")
    @ApiOperationSupport(order = 3, author = "mr.chen")
    @PutMapping("/{id}")
    @AuthToken(rule = "update")
    public ResponseEntity<?> update(@RequestBody Schedule schedule,@PathVariable Long id){
        final Schedule scheduleStore = this.thisService.update(schedule,id);
        return ResponseEntity.ok(scheduleStore);
    }


    @ApiImplicitParam(name = "file", value = "file", required = true,dataType = "MultipartFile",allowMultiple=true)
    @ApiOperation(value = "导入值班记录")
    @ApiOperationSupport(order = 6, author = "mr.chen")
    @PostMapping(value="import",produces={MediaType.ALL_VALUE})
    @AuthToken(rule = "import",require = false)
    public ResponseEntity<?> read(@RequestParam("file") MultipartFile file) {
        try {
            final ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Schedule> schedules = reader.read(0, 2, Schedule.class);
            for(Schedule Schedule:schedules){
                thisService.save(Schedule);
            }
        }catch (IOException e){
            e.getMessage();
        }
        return ResponseEntity.ok("导入成功");
    }

    @ApiImplicitParam(name = "uuid", value = "uuid", required = true,dataType = "String")
    @ApiOperation(value = "下载模板！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @GetMapping(value = "/{uuid}",produces = {MediaType.APPLICATION_OCTET_STREAM_VALUE,MediaType.APPLICATION_ATOM_XML_VALUE})
    public ResponseEntity<?> downloadTemplate(@PathVariable("uuid") String uuid) throws FileNotFoundException {

        final LocalFile byUUID = this.localFileService.getByUuidName(uuid);

        if(byUUID == null){
            throw new BaseException("数据不存在",new ResultResponse("数据不存在", HttpStatus.BAD_REQUEST.value()));
        }

        InputStream inputStream = new FileInputStream(new File(CommonUtils.filePath(uuid+"."+byUUID.getSuffix())));

        InputStreamResource inputStreamResource =new InputStreamResource(inputStream);

        HttpHeaders headers=new HttpHeaders();

        return new ResponseEntity<>(inputStreamResource,headers, HttpStatus.OK);
    }


    @ApiOperation(value = "发送消息！")
    @ApiOperationSupport(order = 1, author = "mr.chen")
    @PostMapping("sendMsg")
    public ResponseEntity<?> sendMsg(){
        final boolean b = this.thisService.sendMessage();
        return ResponseEntity.ok(b);
    }
}